package com.itheima.service.impl;
import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import com.itheima.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

/*
    事务性
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    DataSource dataSource;

    @Autowired
    AccountDao accountDao;

    @Override
    public void transfer(String fromName, String toName, double money) {
        //获取两个账户对象
        Account fromAccount = accountDao.findByName(fromName);
        Account toAccount = accountDao.findByName(toName);

        //更新余额
        fromAccount.setMoney(fromAccount.getMoney() - money);
        toAccount.setMoney(toAccount.getMoney() + money);

        //更新数据库
        accountDao.update(fromAccount);


        accountDao.update(toAccount);
    }
}
